import { type App } from 'vue'
import { message } from './utils/message'
import modal from './plugins/modal'
import vSmoothIn from './directives/v-smooth-in'
const components: Record<string, { default: any }> = import.meta.glob('./components/**/*.vue', {
  eager: true,
})
export default {
  install(app: App<Element>, options = {}) {
    console.log('Vivid-ui is installed')
    registerComponents(app)

    installPlugins(app)

    installDirectives(app)

    // 4. 注入配置项 $config
    provideConfig(app, options)
  },
}

function registerComponents(app: App<Element>) {
  const componentNames: string[] = []
  Object.entries(components).forEach(([path, module]) => {
    const component = module.default
    const name = component.name || (path.split('/').pop() || '').replace(/\.vue$/, '')
    componentNames.push(name)
    app.component(name, component)
  })
  console.log(`register component: ${componentNames}`)
}
function installPlugins(app: App<Element>) {
  console.log('install plugins')
  app.use(modal)
  app.config.globalProperties.$notify = (msg: string, type = 'info') => {
    console.log(`[${type}] ${msg}`)
  }

  app.config.globalProperties.$message = (msg: string, type = 'info', duration = 3000) => {
    console.log(`[${type}] ${msg}`)
    message(msg, type, duration)
  }
}

function installDirectives(app: App<Element>) {
  console.log('install directives')
  app.directive('smooth-in', vSmoothIn)
}

function provideConfig(app: App<Element>, options = {}) {
  console.log('provide config')
  console.log(app, options)
}
